Take-home Exercise 4

Putting Visual Analytics into Practical Use

Author

Affiliation

Zhou Yiou

MITB SMU

Published

Feb. 26, 2022

DOI

1.0 About the task

This take-home exercise requires to apply appropriate data visualization techniques learned in Lesson 6 to accomplish the following tasks:

  1. Script stock prices of top 40 companies in Singapore by market capitalisation between 1st January 2020 - 31st December 2021 by using tidyquant R package.

  2. Using horizon graph to plot historical stock prices by the top 40 companies by market capitalisation.

2.0 Installing and loading the required libraries

In this exercise, tidyquant, tidyverse, ggplot2, dyplyr, and Knitr will be used.

packages = c('tidyverse', 'tidyquant', 'ggplot2','dplyr','tidyr','ggthemes','ggHoriPlot',
             'knitr')
for(p in packages){
  if(!require(p,character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}

3.0 data wrangling

Script data from top 40 companies in Singapore by market capitalisation

Reference: scraping of fiancial dataset

Import the data:

Companies<-read.csv('data/companiesmarketcap.csv')

Prepare the data:

Only the list of symbols was retained for the subsequent use. slice_max() is used to get the top 40 stocks in SGX by marketcap.

top40<-Companies%>%
  slice_max(`marketcap`,n=40)%>%
  select(Symbol,Name)

Script data from Yahoo Finance:

The following code is used to manipulate and select the stock symbols within 1 January 2020 to 31 December 2021 from Yahoo Finance as required. The data was returned in daily intervals. tq_get() function in tidyquant R packages is used to retrieve date, volume, opening, highest, lowest, closing, and adjusted price. group_by() function is used to select() function is used to retain the symbol, name, data and close price column.

from_date = "2020-01-01"
to_date = "2021-12-31"
period_type = "days"  # "days"/ "weeks"/ "months"/ "years"

stock_data_daily<-top40 %>%  
  tq_get(get = "stock.prices",
         from = from_date,
         to = to_date) %>%
  group_by(Symbol,Name)%>%
  select(Symbol,Name,date,close)%>%
  tq_transmute(select = NULL, 
               mutate_fun = to.period, 
               period  = period_type)
rmarkdown::paged_table(stock_data_daily)
ABCDEFGHIJ0123456789
Symbol
<chr>
Name
<chr>
date
<date>
close
<dbl>
SESea (Garena)2020-01-0240.040001
SESea (Garena)2020-01-0340.490002
SESea (Garena)2020-01-0640.480000
SESea (Garena)2020-01-0741.009998
SESea (Garena)2020-01-0840.160000
SESea (Garena)2020-01-0940.529999
SESea (Garena)2020-01-1039.580002
SESea (Garena)2020-01-1340.020000
SESea (Garena)2020-01-1439.939999
SESea (Garena)2020-01-1540.419998

4.0 Data Visualization

Build the horizon plots in ggplot2 using geom_horizon().

stock_data_daily %>% ggplot() +
  geom_horizon(aes(date, close))+
  scale_fill_hcl(palette = 'RdBu', reverse = F) + #set the color palette
  facet_grid(Name~.) +
  theme_few() +
  theme(
    panel.spacing.y=unit(0, "lines"),
    strip.text.y = element_text(size = 7, angle = 0, hjust = 0),
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.border = element_blank()
    ) +
  scale_x_date(expand=c(0,0), 
               date_breaks = "1 month", 
               date_labels = "%b%Y") +
  xlab('Date') +
  ggtitle('Close price of SGX top 40 by marketcap', 
          'from 1/1/2020 to 31/12/2021')

Adjust the size of fond, set the x-axis interval to 2 months.

stock_data_daily %>% ggplot() +
  geom_horizon(aes(date, close))+
  scale_fill_hcl(palette = 'RdBu', reverse = F) + #set the color palette
  facet_grid(Name~.) +
  theme_few() +
  theme(
    panel.spacing.y=unit(0, "lines"),
    strip.text.y = element_text(size = 16, angle = 0, hjust = 0),
    axis.text.x = element_text(size = 14),
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.border = element_blank(), 
    axis.title=element_text(size=20,face="bold"),
    plot.title = element_text(size=28, face = "bold"),
    plot.subtitle = element_text(size=20, face = "bold")
    )+
  scale_x_date(expand=c(0,0), 
               date_breaks = "2 month", 
               date_labels = "%b") +
  xlab('Date') +
  ggtitle('Close price of SGX top 40 by marketcap', 
          'from 1/1/2020 to 31/12/2021')+
   guides(fill= guide_legend(title="stock price +(blue) or -(red)",
                       title.position = "top")
          )

5.0 Peer learning

Inspired by visualizations shown in the class, consider adopting x-axis intersection Line to highlight the timeline regarding covid-19 related events in Singapore and worldwide. To examine the impact of COVID-19 pandemic and companies’ stock price listed in the SGX.

geom_vline() is used to mark the time line.

stock_data_daily %>% ggplot() +
  geom_horizon(aes(date, close))+
  scale_fill_hcl(palette = 'RdBu', reverse = F) + #set the color palette
  geom_vline(xintercept = as.Date("2020-03-19"), linetype = "dotdash", color = "green", size = 0.5) +
  geom_vline(xintercept = as.Date("2020-03-23"), linetype = "dotdash", color = "green", size = 0.5) +
  geom_vline(xintercept = as.Date("2020-04-07"), linetype = "dotted", size = 0.5) +
  geom_vline(xintercept = as.Date("2020-06-01"), linetype = "dotted", size = 0.5) +
  geom_vline(xintercept = as.Date("2020-12-14"), linetype = "dotted", size = 0.5) +
  facet_grid(Name~.) +
  theme_few() +
  theme(
    panel.spacing.y=unit(0, "lines"),
    strip.text.y = element_text(size = 16, angle = 0, hjust = 0),
    axis.text.x = element_text(size = 14),
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.border = element_blank(), 
    axis.title=element_text(size=20),
    plot.title = element_text(size=28, face = "bold"),
    plot.subtitle = element_text(size=20, face = "bold")
    )+
  scale_x_date(expand=c(0,0), 
               date_breaks = "3 month", 
               date_labels = "%b%Y") +
  xlab('Date') +
  ggtitle('Close price of SGX top 40 by marketcap', 
          'from 1/1/2020 to 31/12/2021')+
   guides(fill= guide_legend(title="stock price +(blue) or -(red)",
                       title.position = "top")
          )

6.0 Observations

The covid-19 timeline in Singapore/the world according to straitstimes:

19 March 2020:

23 March 2020:

7 April 2020:

1 Jun 2020:

14 Dec 2020: